package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.FoodKnowledge;
import com.itheima.service.FoodKnowledgeService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/foodknowledge")
public class FoodKnowledgeController {

    @Reference
    private FoodKnowledgeService foodKnowledgeService;

    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            //读取文件内容封装list集合
            List<String[]> list = POIUtils.readExcel(excelFile);
            //判断list集合是否为空
            if(list != null && list.size()>0) {
                //创新新的list集合用于存储预约信息
                List<FoodKnowledge> foodKnowledgeList = new ArrayList<>();
                //遍历文件内容集合
                for (String[] strings : list) {
                    FoodKnowledge foodKnowledge = new FoodKnowledge(null,strings[0],strings[1],strings[2],strings[3]);
                    //将数据存入预约信息集合
                    foodKnowledgeList.add(foodKnowledge);
                }
                //调用新增方法传入预约信息集合
                foodKnowledgeService.add(foodKnowledgeList);
            }
        }catch (Exception e){
            e.printStackTrace();
            //返回上传失败
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        //返回上传成功
        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = foodKnowledgeService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    @RequestMapping("/add")
    //添加
    public Result add(@RequestBody FoodKnowledge foodKnowledge) {
        List<FoodKnowledge> list = new ArrayList<>();
        list.add(foodKnowledge);
        try {
            //调用service方法
            foodKnowledgeService.add(list);
        } catch (Exception e) {
            //失败返回Result对象
            return new Result(false, MessageConstant.ADD_FOODKNOWLEDGE_FAIL);
        }
        //成功返回Result对象
        return new Result(true, MessageConstant.ADD_FOODKNOWLEDGE_SUCCESS);
    }

    @RequestMapping("/findById")
    //根据id查询item信息
    public Result findByName(Integer id) {
        //try
        try {
            //调用service方法，返回checkItem
            FoodKnowledge foodKnowledge = foodKnowledgeService.findById(id);
            //返回查询成功
            return new Result(true, MessageConstant.QUERY_FOODKNOWLEDGE_SUCCESS, foodKnowledge);
        } catch (Exception e) {
            e.printStackTrace();
            //返回查询失败
            return new Result(false, MessageConstant.QUERY_FOODKNOWLEDGE_FAIL);
        }
    }

    @RequestMapping("/edit")
    //编辑
    public Result edit(@RequestBody FoodKnowledge foodKnowledge) {
        //try
        try {
            //调用service方法
            foodKnowledgeService.edit(foodKnowledge);
        } catch (Exception e) {
            //返回编辑失败
            return new Result(false, MessageConstant.EDIT_FOODKNOWLEDGE_FAIL);
        }
        //返回编辑成功
        return new Result(true, MessageConstant.EDIT_FOODKNOWLEDGE_SUCCESS);
    }

    @RequestMapping("/delete")
    //删除
    public Result delete(Integer id) {
        //try
        try {
            //调用service方法
            foodKnowledgeService.delete(id);
        } catch (RuntimeException e) {
            //返回异常错误
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            //返回删除失败
            return new Result(false, MessageConstant.DELETE_FOODKNOWLEDGE_FAIL);
        }
        //返回删除成功
        return new Result(true, MessageConstant.DELETE_FOODKNOWLEDGE_SUCCESS);
    }
}
